package com.pzc.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.List;

import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.query.Query;

import com.luojun.entity.Car;
import com.luojun.entity.User;
import com.luojun.util.HibernateUtil;
import com.pzc.db.DBUtil;
import com.pzc.entity.PersonManage;



public class ManageDao {
	 
	protected PreparedStatement preparedStatement;
	protected ResultSet resultSet;
	
	public ManageDao(String nickname, String headportraint, String address, String gender, String birthday) {
		// TODO Auto-generated constructor stub
	}
	
	public ManageDao() {
		super();
	}
	public void del(int id){
		Session session = null;
		Transaction transaction = null;
		PersonManage personManage = null;
		try {
			session = HibernateUtil.openSession();
			transaction = session.beginTransaction();
			personManage=	session.get(PersonManage.class, id);
			session.delete(personManage);
			 transaction.commit();// 5.提交事务
		    } catch (Exception e) {
		      e.printStackTrace();
		      transaction.rollback();
		    } finally {
		      if (null != session) {
		    	//6.关闭session
		        session.close();
		      }
		     
		    } 
	}

	//根据ID查其他东西
	public PersonManage selectManage(int userId) {
		Session session = null;
		Transaction transaction = null;
		PersonManage personManage = null;
		try {
			session = HibernateUtil.openSession();
			transaction = session.beginTransaction();
			 Query q = session.createQuery(" from PersonManage pm  where pm.userid=? ");
			q.setInteger(0, userId);
	
			 if(q.list().get(0) != null){ //只查看获得的数据list里的第一条数据（下表为0）
				 personManage=(PersonManage)q.list().get(0);
				 return personManage ;
			 }
			 transaction.commit();// 5.提交事务
		    } catch (Exception e) {
		      e.printStackTrace();
		      transaction.rollback();
		    } finally {
		      if (null != session) {
		    	//6.关闭session
		        session.close();
		      }
		     
		    } 
		    return null;
	}
	
	public void updateManage(PersonManage pm) {
		Session session = null;
		Transaction transaction = null;
		User user = null;
			session = HibernateUtil.openSession();
			transaction = session.beginTransaction();
			Query query = session.createQuery("from PersonManage m where m.userid=? ");
			query.setInteger(0, pm.getUserid());
			PersonManage p =(PersonManage)query.list().get(0);
			p.setNickname(pm.getNickname());
			p.setAddress(pm.getAddress());
			p.setBirthday(pm.getBirthday());
			p.setGender(pm.getGender());
			session.update(p);
			System.out.println("修改成功");
			transaction.commit();

	}
	public List<PersonManage> find(){
		Session session = null;
		session = HibernateUtil.getCurrentSession();
		Query query= session.createQuery("from PersonManage");
		List<PersonManage> list = query.list();
		return list;
	}
}